package cc.z8g.browser

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import cc.z8g.browser.utils.ThemeUtils
import cc.z8g.browser.utils.sp.UserSp

abstract class ThemableSettingsActivity : AppCompatActivity() {

    private var themeId: Int = UserSp.THEME_LIGHT

    override fun onCreate(savedInstanceState: Bundle?) {
        themeId = UserSp.getInt(UserSp.KEY_THEME)
        // set the theme
        when (themeId) {
            UserSp.THEME_DARK -> {
                setTheme(R.style.Theme_SettingsTheme_Dark)
                window.setBackgroundDrawable(ColorDrawable(ThemeUtils.getPrimaryColorDark(this)))
            }

            UserSp.THEME_BLACK -> {
                setTheme(R.style.Theme_SettingsTheme_Black)
                window.setBackgroundDrawable(ColorDrawable(ThemeUtils.getPrimaryColorDark(this)))
            }

            else -> {
                setTheme(R.style.Theme_SettingsTheme)
                window.setBackgroundDrawable(ColorDrawable(ThemeUtils.getPrimaryColor(this)))
            }
        }

        super.onCreate(savedInstanceState)

        resetPreferences()
    }

    private fun resetPreferences() {
        window.statusBarColor = if (UserSp.getBoolean(UserSp.KEY_BLACK_STATUS_BAR)) Color.BLACK else ThemeUtils.getStatusBarColor(this)
    }

    override fun onResume() {
        super.onResume()
        resetPreferences()
        if (UserSp.getInt(UserSp.KEY_THEME) != themeId) {
            recreate()
        }
    }

}
